<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use App\Http\Controllers\Controller;
use App\Models\Products;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class ProductController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

        $keyword=$request->input('keyword');
//        $where=[];
//        if(!empty($keyword)){
//            $where[]=['username'=>'like',"%$keyword%"];
//        }
        $data= Products::when($keyword,function($query) use($keyword){
            $query->where('name','like',"%$keyword%")->get();
        })->with(['cate'])->paginate($request->input('limit',10));

        $totalMoney=Products::sum('sale');

        return $this->success(compact('data','totalMoney'));

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $request->offsetSet('banners',implode(',',$request->input('banners')));
        if(Products::create($request->all())) return $this->success();
        $this->error();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
       $product= Products::find($id);

       return $this->success($product);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        if($request->input('banners')){
            $request->offsetSet('banners',implode(',',$request->input('banners')));
        }

        if(Products::where('id',$id)->update($request->all())) return $this->success();
        $this->error();
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        if(Products::destroy($id)) return $this->success();
        $this->error();
    }

    /**
     * 商品数据分析
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function analyse(Request $request)
    {
        $keyword=$request->input('keyword');
        $time=$request->input('time');
        $res=[1=>'sale_num|desc',2=>'sale_num|asc',3=>'browse|desc',4=>'browse|asc'];
       $data= DB::table('products')
           ->when($keyword,function ($query) use($keyword,$res) {
               foreach ($res as $k=>$v){
                   if($k==$keyword){
                       $query->orderBy(explode('|',$v)[0],explode('|',$v)[1]);
                   }
               }
           } )
           ->when($time,function ($query) use($time){
                $start=strtotime(explode(',',$time)[0]);
                $end=strtotime(explode(',',$time)[1]);
               $query->whereBetween('created_at',[$start,$end]);
           })
           ->offset($request->input('page',1))->limit($request->input('limit',10))->get();
       $total=DB::table('products')->count();
        foreach ($data as &$item){
            $item->total=sprintf("%.2f",$item->sale_num*$item->sale); //保留两位小数，不四舍五入
        }
        return $this->success(compact('data','total'));
    }
}
